package com.qf.mybatisplus.utils;

import com.lowagie.text.BadElementException;
import com.lowagie.text.pdf.BarcodePDF417;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class BarcodeUtil {
    public static void main(String[] args) throws IOException, BadElementException {
//        String codeString = "GB0626-2005^GD000014001300000871^广东省**单位^通知^粤**函〔2013〕1**号^**单位^***标题^无^无^20131031^^打字室^20131031^^|";
//        String codePath="d:/pdf417.bmp";
//        String A=createPdf417(codeString,codePath);
//        System.out.println(A);
//        BarcodeUtil.createPdf417("xxxxxxx","E:\\houhou\\提交svn\\工具\\twst\\" + "ewm/" + StringUtils.getUUID() + ".png");
        BarcodeUtil.changeSize(500,100,"D:\\moko\\nginx-1.20.1\\html\\MOKO-IMG\\resultPDF\\2021686139.png");

    }

    public static String createPdf417(String codeString,String codePath){
        BarcodePDF417 pdf = new BarcodePDF417();
        try {
            pdf.setText(codeString.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Image pdfImg = pdf.createAwtImage(Color.black, Color.white);
        BufferedImage img = new BufferedImage((int)pdfImg.getWidth(null), (int)pdfImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics g = img.getGraphics();

        g.drawImage(pdfImg, 0, 0, Color.white, null);
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(codePath));
            ImageIO.write(img, "PNG", os);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String base64= Base64Util.imageToBase64(codePath);
        return base64;
    }

    /**
     * 转二维条码
     *
     *
     *
     * */
    public static void createPDF417Code (String path, String codeString) throws IOException {
        System.out.println("xxxxxxxxxxxxxxxxxxx");
        BarcodePDF417 pdf = new BarcodePDF417();
        //pdf.setYHeight(30);
        //pdf.setLenCodewords(500);
        pdf.setText(codeString.getBytes("gbk"));
        Image pdfImg = pdf.createAwtImage(Color.black, Color.white);

        BufferedImage img = new BufferedImage((int)pdfImg.getWidth(null),
                (int)pdfImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics g = img.getGraphics();
        g.drawImage(pdfImg, 0, 0, Color.white, null);
        System.out.println("path======>" + path);
        OutputStream os = new BufferedOutputStream(new FileOutputStream(path));
        ImageIO.write(img, "PNG", os);
        System.out.println("ok----------------------------------------");
        changeSize(300, 100, path);
        //return Base64Util.imageToBase64(path);
    }

    /**
     * 改变图片的尺寸
     *
     * @param newWidth, newHeight, path
     * @return boolean
     */
    public static boolean changeSize(int newWidth, int newHeight, String path) {
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(path));

            //字节流转图片对象
            Image bi = ImageIO.read(in);
            //构建图片流
            BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            //绘制改变尺寸后的图
            tag.getGraphics().drawImage(bi, 0, 0, newWidth, newHeight, null);
            //输出流
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(path));
            //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            //encoder.encode(tag);
            ImageIO.write(tag, "PNG", out);
            in.close();
            out.close();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
}
